package com.anuprayog.convertunits;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class VelocityActivity extends Activity {

	private EditText text, result;
	private Spinner spin1, spin2;
	private TextView resultString, resultString1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_velocity);
		
		text = (EditText) findViewById(R.id.editText1);
		spin1 = (Spinner) findViewById(R.id.spinner1);
		spin2 = (Spinner) findViewById(R.id.spinner2);
		result = (EditText) findViewById(R.id.editText2);
		resultString = (TextView) findViewById(R.id.textView4);
		resultString1 = (TextView) findViewById(R.id.textView5);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.velocity, menu);
		return true;
	}

	/*@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}*/

	//final Button button = (Button) findViewById(R.id.button1);
    // button.setOnClickListener(new View.OnClickListener() {
    	
	public void onClick(View view) {
			if (text.getText().length() == 0) {
				Toast.makeText(this, "Please enter a valid number",
						Toast.LENGTH_LONG).show();
				return;
			}

			double input = Double.parseDouble(text.getText().toString());
			String unit1 = spin1.getSelectedItem().toString();
			String unit2 = spin2.getSelectedItem().toString();
			double mpsvalue;
			double d;
			
			if (unit1.equals("Meter / Second"))
			{
				mpsvalue = input;
			}
			
			else if (unit1.equals("Meter / Minute"))
			{
				mpsvalue = input*(1/60d);
			}
			
			else if (unit1.equals("Meter / Hour"))
			{
				mpsvalue = input*(1/3600d);
			}
			
			else if (unit1.equals("Kilometer / Second"))
			{
				mpsvalue = input*(1000);
			}
			
			else if (unit1.equals("Kilometer / Minute"))
			{
				mpsvalue = input*(50/3d);
			}
			
			else if (unit1.equals("Kilometer / Hour"))
			{
				mpsvalue = input*(5/18d);
			}
			
			else if (unit1.equals("Miles / Second"))
			{
				mpsvalue = input*(1609.344);
			}
			
			else if (unit1.equals("Miles / Minute"))
			{
				mpsvalue = input*(1609.344/60);
			}
			
			else if (unit1.equals("Miles / Hour"))
			{
				mpsvalue = input*(1609.344/3600);
			}
			
			else if (unit1.equals("Foot / Second"))
			{
				mpsvalue = input*(0.3048);
			}
			
			else if (unit1.equals("Foot / Minute"))
			{
				mpsvalue = input*(0.3048/60);
			}
			
			else if (unit1.equals("Foot / Hour"))
			{
				mpsvalue = input*(0.3048/3600);
			}
			
			else if (unit1.equals("Yard / Second"))
			{
				mpsvalue = input*(0.9144);
			}
			
			else if (unit1.equals("Furlong / Fortnight"))
			{
				mpsvalue = input*(201.168/1209600);
			}
			
			else if (unit1.equals("Knot"))
			{
				mpsvalue = input*(463/900d);
			}
			
			else if (unit1.equals("Speed of Sound"))
			{
				mpsvalue = input*(343.2);
			}
			
			else if (unit1.equals("Speed of Light"))
			{
				mpsvalue = input*(299792458);
			}
			
			else
			{
				mpsvalue = 0;
			}
			
			
			if (unit2.equals("Meter / Second")){
				d = mpsvalue;
			}
				
			else if (unit2.equals("Meter / Minute")){
				d = mpsvalue*(60);
			}
			
			else if (unit2.equals("Meter / Hour")){
				d = mpsvalue*(3600);
			}
			
			else if (unit2.equals("Kilometer / Second")){
				d = mpsvalue*(0.001);
			}
			
			else if (unit2.equals("Kilometer / Minute")){
				d = mpsvalue*(0.06);
			}
			
			else if (unit2.equals("Kilometer / Hour")){
				d = mpsvalue*(3.6);
			}
			
			else if (unit2.equals("Miles / Second")){
				d = mpsvalue*(1/1609.344);
			}
			
			else if (unit2.equals("Miles / Minute")){
				d = mpsvalue*(60/1609.344);
			}
			
			else if (unit2.equals("Miles / Hour")){
				d = mpsvalue*(3600/1609.344);
			}
			
			else if (unit2.equals("Foot / Second")){
				d = mpsvalue*(1/0.3048);
			}
			
			else if (unit2.equals("Foot / Minute")){
				d = mpsvalue*(60/0.3048);
			}
			
			else if (unit2.equals("Foot / Hour")){
				d = mpsvalue*(3600/0.3048);
			}
			
			else if (unit2.equals("Yard / Second")){
				d = mpsvalue*(1/0.9144);
			}
			
			else if (unit2.equals("Furlong / Fortnight")){
				d = mpsvalue*(1209600/201.168);
			}
			
			else if (unit2.equals("Knot")){
				d = mpsvalue*(900/463d);
			}
			
			else if (unit2.equals("Speed of Sound")){
				d = mpsvalue*(1/343.2);
			}
			
			else if (unit2.equals("Speed of Light")){
				d = mpsvalue*(1/299792458d);
			}
			
			else
			{
				d = 0;
			}
		
			result.setText(String.valueOf(d));
			
			resultString.setText(String.valueOf(input) + " " + unit1 + " =");
			resultString1.setText(String.valueOf(d) + " " + unit2);
			
		}
	
	
	public void goBack(View view) {
		finish();
	}
		
	public void goSwap(View view) {
		int spin1Index = spin1.getSelectedItemPosition();

		spin1.setSelection(spin2.getSelectedItemPosition());
		spin2.setSelection(spin1Index);
	}
}
